//
//  CommonUtil.m
//  ios_news
//
//  Created by Han on 14-6-19.
//  Copyright (c) 2014年 ucoder. All rights reserved.
//

#import "Util.h"
#import "MBProgressHUD+Add.h"

@implementation Util

+ (NSString *)getDateFromTimestamp:(NSString *)time
{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[time doubleValue]];
    NSDateFormatter *fm = [[NSDateFormatter alloc] init];
    //[fm setDateFormat:@"yyyy-MM-dd HH:mm"];
    [fm setDateFormat:@"MM-dd HH:mm"];
    return [fm stringFromDate:date];
}


+ (void)dismissSuccess:(NSString *)mess inView:(UIView *)view
{
    [MBProgressHUD showSuccess:mess toView:view];
}

+ (void)dismissError:(NSString *)mess inView:(UIView *)view
{
    [MBProgressHUD showError:mess toView:view];
}

+ (void)showMess:(NSString *)mess inView:(UIView *)view
{
    [MBProgressHUD showHUDAddedTo:view animated:YES];

}

+ (void)dissmissProcessInView:(UIView *)view
{
    [MBProgressHUD hideHUDForView:view animated:YES];
}


+ (BOOL)isNightMode
{
    return [[NSUserDefaults standardUserDefaults] boolForKey: @"isNightMode"];
}

#pragma mark - 替换 HTML 标签
+(NSString *)formatHtmlString:(NSString *) htmlString
{

    NSScanner * scanner = [NSScanner scannerWithString:htmlString];
    NSString * text = nil;
    while([scanner isAtEnd]==NO)
    {
        //找到标签的起始位置
        [scanner scanUpToString:@"<" intoString:nil];
        //找到标签的结束位置
        [scanner scanUpToString:@">" intoString:&text];
        //替换字符
        htmlString = [htmlString stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>",text] withString:@""];
    }
    NSString * regEx = @"<([^>]*)>";
    htmlString = [htmlString stringByReplacingOccurrencesOfString:regEx withString:@""];
    
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"<p>" withString:@""];
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"</p>" withString:@""];
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"<br/>" withString:@""];
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"<br />" withString:@""];
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"<div>" withString:@""];
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"</div>" withString:@""];
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"&nbsp;" withString:@""];
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@" " withString:@""];
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"&mdash;" withString:@"--"];
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"&ldquo;" withString:@"“"];
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"&rdquo;" withString:@"”"];
    return htmlString;
}


//获取文字的高度
+ (float)getTextHeightWithString:(NSString*)string
                            font:(UIFont *)font
                        andWidth:(float)width;
{
    CGSize constraint = CGSizeMake(width, CGFLOAT_MAX);
    //
    NSDictionary * attributes = [NSDictionary dictionaryWithObject:font
                                                            forKey:NSFontAttributeName];
    
    NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:string
                                                                         attributes:attributes];
    
    CGRect rect = [attributedText boundingRectWithSize:constraint
                                               options:NSStringDrawingUsesLineFragmentOrigin
                                               context:nil];
    CGSize size = rect.size;
    
    //计算高度
    //CGSize size = [string sizeWithFont:font constrainedToSize:constraint];
    
    return size.height;
}


+ (void)saveObject:(NSObject *)obj withFileName:(NSString*)cacheKey
{
    
    if(obj == nil) {
        return;
    }
    
    NSMutableData *data = [[NSMutableData alloc] init];
    
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    
    [archiver encodeObject:obj forKey:cacheKey];
    
    [archiver finishEncoding];
    
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    
    [data writeToFile:[path stringByAppendingPathComponent:cacheKey] atomically:YES];
    
}

+ (NSObject *)readObject:(NSString *)cacheKey
{
    
    NSObject *obj = nil;
    
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSData *data = [[NSMutableData alloc] initWithContentsOfFile: [path stringByAppendingPathComponent:cacheKey]];
    
    if(data) {
        NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
        obj = [unarchiver decodeObjectForKey:cacheKey];
    }
    
    return obj;
    
}

@end
